<template>
	<el-dialog :visible.sync="options.showDialog" width="800px" class="lead-dialog">
		<h2>导入相应格式的Excel表格</h2>
		<p>导入的表格中包含入职人员【姓名/手机号码】</p>
		<p>【员工自行入职奖励/门店送入职奖励/其它门店送入职奖励】</p>
		<img src="../../assets/img/importViewImg.png" alt="" style="width: 90%;margin: 10px 5%;">
		<el-upload class="upload-demo" action="#" :show-file-list="false" :before-upload="beforeAvatarUpload" :http-request="uploadOk">
			<el-button size="small" type="primary">一键导入</el-button>
		</el-upload>
	</el-dialog>
</template>

<script>
export default{
	props:{
		options:{
			type: Object,
			require: true
		}
	},
	methods:{
		//文件上传前校验
		beforeAvatarUpload(file){
			console.log(file);
			let Xls = file.name.split('.');
			if(Xls[1] === 'xls'||Xls[1] === 'xlsx'){
				return file
			}else {
				this.$message.error('上传文件只能是 xls/xlsx 格式!')
				return false
			}
		},
		//上传成功
		uploadOk(val){
			let reader = new FileReader();
			FileReader.prototype.readAsBinaryString = f => {
				let binary = "";
				let data = '';
				let outdata = '';
				reader.onload = (e)=> {
					let bytes = new Uint8Array(reader.result);
					let length = bytes.byteLength;
					for (let i = 0; i < length; i++) {
						binary += String.fromCharCode(bytes[i]);
					}
					let XLSX = require("xlsx");
					data = XLSX.read(binary, {type: "binary"});
					outdata = XLSX.utils.sheet_to_json(data.Sheets[data.SheetNames[0]]);
					this.conversion(outdata);
				}
				reader.readAsArrayBuffer(val.file);
			}
			reader.readAsBinaryString(val.file);
		},
		//数据格式转换
		conversion(arr){
			// console.log(arr);
			let list = [];
			if(arr && arr.length > 0){
				arr.forEach(item => {
					let nav = {};
					nav.nextusername = item['姓名'];
					nav.telnum = item['手机号码'];
					nav.interalnum = item['员工自行入职奖励'];
					nav.manageInteralnum = item['门店送入职奖励'];
					nav.operator = JSON.parse(localStorage.getItem('userinfo')).username;
					list.push(nav);
				})
			}
			// console.log(list);
			this.$storesAxios.post('/v1/shop/selectReturnCostListExcel',{returnCostListBean: list}).then(res => {
				console.log(res);
				if(res.data.code === 1){
					let result = res.data.data;
					result.forEach(val => {
						if(val.toLead){
							val.result = '成功';
						}else{
							val.result = '失败';
							val.shopName = '-';
							val.companyname = '-';
							val.positionname = '-';
							val.entrydate = '-';
						}
					})
					this.$emit('openImportResult',result);
				}else{
					this.$message.warning(res.data.message);
				}
			})
		}
	}
}
</script>

<style scoped="scoped">
.lead-dialog{text-align: center;}
.lead-dialog p{line-height: 24px;color: red;}
.lead-dialog h2{font-weight: 600;line-height: 40px;padding: 20px 0;}
.upload-demo{padding: 20px;}
.upload-btn{}
</style>
